<template>
	<view id="selector">
		<select-practitioner
									:placeholderText="placeholderText"
									:dataList="projectList"
									:areaList="areaList"
									:fieldName="fieldName"
									DeputyFieldName="constructorName"
									:isSearch="true"
									:isArea="true"
									@selectChange="selectChange"
									@onTab="onTab"
							>
							</select-practitioner>
	</view>
</template>

<script>
	import { getAreaList, getChildren, getProjectIndex } from '@/api/global'
	import { AREA_ID } from '@/store/mutaction-types'
	export default {
		data () {
			return {
				areaId: uni.getStorageSync(AREA_ID),
				placeholderText: '请输入项目名称查询',
				fieldName: 'projectName',
				tab: 0,
				fromUrl: '',
				toUrl: '',
				projectId: '',
				projectName: '',
				projectList: [],
				areaList: []
			}
		},
		onLoad(option) {
			this.fromUrl = option.from
			this.toUrl = option.to
		},
		onShow () {
			this.getProjectList()
			this.getAreaList()
		},
		methods: {
			getProjectList () {
				getProjectIndex().then(res => {
					const data = res.data.data
					let projectList = []
					for(const k in data) {
						projectList = projectList.concat(data[k])
					}
					this.projectList = projectList
					// this.isPop = false
				})
			},
			getAreaList () {
				getAreaList().then(res => {
					const data = res.data.data
					const area = []
					for (const k in data) {
						if (data[k].projectCnt !== 0) {
							area.push(data[k])
						}
					}
					this.areaList = area
				})
				// getChildren(this.areaId).then(res => {
				// 	const data = res.data.data
				// 	const area = []
				// 	for (const k in data) {
				// 		if (data[k].parentId === 8) {
				// 			data[k].sonArea = []
				// 			area.push(data[k])
				// 		}
				// 	}
				// 	for (const k in area) {
				// 		for (const j in data) {
				// 			if (area[k].id === data[j].parentId) {
				// 				area[k].sonArea.push(data[j])
				// 			}
				// 		}
				// 	}
				// 	this.areaList = area
				// 	// let area = []
				// 	// area = this.groupBy(data, res => {
				// 	// 	return res.parentId
				// 	// })
				// 	// console.log(area)
				// })
			},
			groupBy (arr, fn) {
				const group = {}
				arr.map(item => {
					const type = JSON.stringify(fn(item))
					group[type] = group[type] || []
					group[type].push(item)
				})
				return Object.keys(group).map(item => {
						return group[item]
				})
			},
			onTab (e) {
				this.tab = e
				if (e === 1) {
					this.placeholderText = '请输入区域名称查询'
				} else {
					this.placeholderText = '请输入项目名称查询'
				}
			},
			selectChange (e) {
				this.projectId = e.id
				this.projectName = e.projectName
				// 工程-项目总数-
				if (this.toUrl === 'projectdetail') {
					uni.navigateTo({
						url: `/packageE/project-info/projectdetail?id=${this.projectId}`,
					})
				}
				// 工程-扬尘管控
				if (this.toUrl === 'dust') {
					uni.navigateTo({
						url: `/packageE/dust-control/dust-control?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-噪声管控
				if (this.toUrl === 'noise') {
					uni.navigateTo({
						url: `/packageE/noise-control/noise-control?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-裸土覆盖
				if (this.toUrl === 'soil') {
					uni.navigateTo({
						url: `/packageE/soil/soil?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-车辆冲洗
				if (this.toUrl === 'wash') {
					uni.navigateTo({
						url: `/packageB/wash/wash?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-上级交办详情
				if (this.toUrl === 'assigned-by-superior') {
					uni.navigateTo({
						url: `/packageE/assigned-by-superior/assigned-by-superior?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-扬尘管控详情
				if (this.toUrl === 'dust-control') {
					uni.navigateTo({
						url: `/packageE/dust-control/dust-control?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-扬尘管控详情
				if (this.toUrl === 'noise-control') {
					uni.navigateTo({
						url: `/packageE/noise-control/noise-control?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-移动办公-项目自查
				if (this.toUrl === 'project-self') {
					uni.navigateTo({
						url: `/packageE/project-self-inspection/project-self-inspection?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
				// 工程-移动办公-问题整改
				if (this.toUrl === 'problem') {
					uni.navigateTo({
						url: `/packageE/problem-rectification/problem-rectification?id=${this.projectId}&projectName=${this.projectName}`,
					})
				}
			},
			selectArea (e) {
				console.log(e)
			}
		}
	}
</script>

<style>
</style>